using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using eShop.Business;
using ECommerce.Library.Web;
using ECommerce.Library;
using eShop.Utility;
using eShop.Business;

public partial class login : SitePublicBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
       if (!String.IsNullOrEmpty(Request["ReturnUrl"]))
            SessionManager.ReturnUrl = Request["ReturnUrl"];
       
        if (!Page.IsPostBack)
        {
           
            hplForgotPassword.NavigateUrl = CustomSiteMap.Recover;
            if (SessionManager.IsUserLoggedIn)
            {
                if (SessionManager.ReturnUrl != null)
                {
                   string returnUrl = SessionManager.ReturnUrl;
                    SessionManager.ReturnUrl = null;
                    Response.Redirect(returnUrl);
                }
                else
                {
                    Response.Redirect(CustomSiteMap.UserHome);
                }
            }
            if (CookieManager.IsCookieExists(CookieManager.USER_NAME))
            {
                string emailAddress = CookieManager.GetCookieValue(CookieManager.USER_NAME);
                string password = CookieManager.GetCookieValue(CookieManager.PASSWORD);
                cbxRemember.Checked = true;
                LoginUser(emailAddress, password);
            }

        }
    }
    protected void LoginUser(string emailAddress, string password)
    {
        if (UserManager.Login(emailAddress, password, UserLocationType.FrontEnd))
        {
            if (cbxRemember.Checked)
            {
                CookieManager.SetCookieValue(CookieManager.USER_NAME, emailAddress, DateTime.Now.AddDays(SiteSettings.CookieTimeOut));
                CookieManager.SetCookieValue(CookieManager.PASSWORD, password, DateTime.Now.AddDays(SiteSettings.CookieTimeOut));
            }
            else
            {
                CookieManager.RemoveCookie(CookieManager.USER_NAME);
                CookieManager.RemoveCookie(CookieManager.PASSWORD);
            }

            if (SessionManager.ReturnUrl != null)
            {
                string returnUrl = SessionManager.ReturnUrl;
                SessionManager.ReturnUrl = null;
                Response.Redirect(returnUrl);
            }
            else
            {
                Response.Redirect(CustomSiteMap.UserHome);
            }
        }
        else
        {
            litMessage.Text = "Login E-Mail Address/Password is not correct.Please enter correct E-Mail Address/Password and try again.";
            divMessage.Attributes.Remove("class");
            divMessage.Attributes.Add("class", "error");

        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        try
        {
            string emailAddress = tbxEmailAddress.Text.Trim();
            string password = tbxPassword.Text.Trim();
            if (RegExUtil.IsEmail(emailAddress) && RegExUtil.IsPassword(password))
            {
                LoginUser(emailAddress, password);
            }
            else
            {
                litMessage.Text = "Login E-Mail Address/Password format is not correct.Please try again.";
                divMessage.Attributes.Remove("class");
                divMessage.Attributes.Add("class", "error");
            }
        }
        catch (Exception ex)
        {
            litMessage.Text="Sign In failed. Please try again."+ex.Message;
            divMessage.Attributes.Remove("class");
            divMessage.Attributes.Add("class","error");
            
        }
    }
    protected void btnCreateAccount_Click(object sender, EventArgs e)
    {
        Response.Redirect(CustomSiteMap.Register);
    }
   
}
